<template>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="queryParams.createDate"
            value-format="yyyy-MM-dd"
            align="right"
            type="date"
            range-separator="-"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-close"
            size="mini"
            @click="handleClose"
            >关闭</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
  
      <el-table
        v-loading="loading"
        :data="workList"
        @selection-change="handleSelectionChange"
      >
      <el-table-column label="作品ID" align="center" prop="workId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="分类的ID" align="center" prop="categoryId" />
      <el-table-column label="作品标题" align="center" prop="title" />
      <el-table-column label="图片地址" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="使用人数" align="center" prop="userNumber" />
      <el-table-column label="点赞数" align="center" prop="likeNumber" />
      <el-table-column label="收藏数" align="center" prop="collectNumber" />
      <el-table-column label="对话次数" align="center" prop="chatNumber" />
      <el-table-column label="作品的内容" align="center" prop="content" />
        
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              v-show="show"
              size="mini"
              type="text"
              icon="el-icon-circle-close"
              @click="cancelAuthUser(scope.row)"
              v-hasPermi="['ai:work:remove']"
              >取消/删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </template>
  
  <script>
  import {
    listCollect,
    addCollect,
    delCollect,
    listHistory,
    addHistory,
    delHistory
  } from "@/api/ai/work";
  
  export default {
    name: "Collect_History",
    data() {
      return {
        // 非单个禁用
        show: true,
        // 遮罩层
        loading: true,
        // 选中用户组
        userIds: [],
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户表格数据
        workList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userId: undefined,
          roleId: undefined,
          userName: undefined,
          phonenumber: undefined,
          createDate:"",
        },
        path:"",
      };
    },
    created() {
      this.queryParams.userId = this.$route.params && this.$route.params.userId;
      this.path=this.$route.path;
      this.getList();
    },
    methods: {
      /** 查询授权用户列表 */
      getList() {
        this.loading = true;
        if (this.path.includes("collect")) {
            listCollect(this.queryParams).then((response) => {
            this.workList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        } else if (this.path.includes("history")) {
            listHistory(this.queryParams).then((response) => {
            this.workList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      },
      // 返回按钮
      handleClose() {
        const obj = { path: "/system/user" };
        this.$tab.closeOpenPage(obj);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.userIds = selection.map((item) => item.userId);
        this.multiple = !selection.length;
      },
      /** 打开授权用户表弹窗 */
      openSelectUser() {
        this.$refs.select.show();
      },
      /** 取消授权按钮操作 */
      cancelAuthUser(row) {
        const userId = this.userId;
        const path=this.path
        if (path.includes("collect")) {
            this.$modal
          .confirm('确认要取消收藏该作品"' + row.title + '"吗？')
          .then(function () {
            return delCollect({ userId: userId, workId: row.workId });
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("取消收藏成功");
          })
          .catch(() => {});
        } else if (this.path.includes("history")) {
            this.$modal
          .confirm('确认要删除关于该作品"' + row.title + '"的记录吗？')
          .then(function () {
            return delHistory({ userId: userId, workId: row.workId });
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除记录成功");
          })
          .catch(() => {});
        }
      },
    },
  };
  </script>
  